//
//  BusinessWhsEditWhsViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 16/1/26.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "BusinessWhsEditWhsViewController.h"

#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "HttpRequest.h"


#import "BusinessWhsEditWhsViewController.h"
@interface BusinessWhsEditWhsViewController ()<UISearchBarDelegate,UITextFieldDelegate>

@end

@implementation BusinessWhsEditWhsViewController

{
    UISearchBar *search;
    HAODEsingleton* commdity;
    NSMutableArray*code;
    NSMutableArray*content;
    UITextField*whsTextFied;
    
}

@synthesize alt = alt;


-(void)viewDidLoad{
    
    [super viewDidLoad];
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
       UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    self.title=self.titleName;
    commdity=[HAODEsingleton shared];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
 
    
    
    
    
    
    
    
    self.view.backgroundColor=zuiqianhuicolor;
    
    UIButton*tianjiaBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
    [tianjiaBtn setTitleColor:luseColor forState:UIControlStateNormal];
    [tianjiaBtn setTitle:@"保存" forState:UIControlStateNormal];
    tianjiaBtn.titleLabel.font=[UIFont systemFontOfSize:14];
    [tianjiaBtn addTarget:self action:@selector(addProduct:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:tianjiaBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;

    
    
    
    
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 64+20, ScreenWidth, 50)];
    view.backgroundColor=[UIColor whiteColor];
    view.layer.borderWidth = 0.35;
    view.layer.borderColor = qianhuicolor.CGColor;
    [self.view addSubview:view];
    
    
    UILabel* Address=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, ScreenWidth*170/750,50)];
    Address.text=[NSString stringWithFormat:@"%@", @"名称"];
    Address.font=[UIFont systemFontOfSize:contenfot];
    Address.textColor=[UIColor blackColor];
    [view addSubview:Address];
    whsTextFied=[[UITextField alloc]initWithFrame:CGRectMake(Address.right-15, 0, ScreenWidth*550/750,  50)];
    whsTextFied.borderStyle = UITextBorderStyleNone;
    whsTextFied.font = [UIFont systemFontOfSize: contenfot];
    whsTextFied.textColor=[UIColor blackColor];
    whsTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
   
    whsTextFied.placeholder = @"请输入仓库名称"; //默认显示的字
    if ([self.sat isEqualToString:@"U"]) {
         whsTextFied.text=commdity.whsName;
    }
   
    [whsTextFied setUserInteractionEnabled:YES];
    whsTextFied.delegate = self;
    whsTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
    [view addSubview:whsTextFied];
    UIView* myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:daxiao];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];
    
    whsTextFied.inputAccessoryView = myToolBar;


    
    
}

-(void)OnTapDone{
    
    [whsTextFied resignFirstResponder];

    
}

-(void)addProduct:(UIButton*)sender{
 
    
    if ([whsTextFied.text length] < 1)
    {
        [self showAlertMessage:@"请输入仓库名称"];
    }
    else
    {
        
 
            
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString* Token = [user objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            
            [params setValue:Token forKey:@"tokenId"];
            [params setValue:self.sat forKey:@"editType"];
        if ([self.sat isEqualToString:@"U"]) {
             [params setValue:commdity.whsId forKey:@"id"];
        }
        
            [params setValue:whsTextFied.text forKey:@"whsName"];
            
            [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
            
            
            [HttpRequest requestData:@"Business/Whs/EditWhs" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    
                    NSLog(@"33333333====%@",severDataDict.dataDict);
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                                [self.navigationController popViewControllerAnimated:YES];
                        
                
                        
                    }else{
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                    }
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    
                    [Global showTextHudSub:@"连接失败" view:self.view];
                }
                
            }];
            
            
            
      
    }

    
    
    
    
    
    
    
    
    
    
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg

{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

@end
